<?php
class sfCreatixMenu extends sfWebRequest
{
    public $menu_yml;
    public $container;
    public $direction;
    private $config_menu = array();
    private $menus       = array();
    private $sf_user;

    public function __construct($sf_user, $menu, $container = 'menu', $direction = 'h', $theme = 'nav')
    {
		$this->container = $container;
    	$this->menu_yml = $menu.'.yml';
    	$this->direction = $direction;
        $this->sf_user = $sf_user;
        $arquivoYml = sfConfig::get('sf_CreatixMenu_yml') . $this->menu_yml;
        if(!file_exists($arquivoYml)) {
            trigger_error("Arquivo de configuração do menu '$arquivoYml' não encontrado");
        }
        $yf = sfYaml::load( sfConfig::get('sf_CreatixMenu_yml') . $this->menu_yml);
       	$this->menus = $yf['menus'];

       	return $this->render($theme);
    }

    public function setMenu($menu)
    {
    	$this->menu_yml = $menu;
    }

    public function render($theme = 'nav')
    {
        $retorno = $this->criarMenus($this->menus);
        $html_menu = "<ul class=\"{$theme}\">";
        $html_menu.= $retorno;
        $html_menu.= "</ul>";

        echo $html_menu;
    }

    private function criarMenus($arr_menus, $parent = true)
    {
        $retorno = "";

        foreach ($arr_menus as $menu)
        {
            // verifico se tem submenu
            $class = '';
            if(isset($menu['submenus']))
            	$class = "class=\"dropdown\" ";
            $retorno.= "<li {$class}>";
            
            // url
            $url = "";
            $module = $this->getArrayValue($menu, 'module');
            $action = $this->getArrayValue($menu, 'action');
            
        	if ($module != '' && $action == '')
                $url = url_for("$module");
            elseif($module != '' && $action != '')
            	$url = url_for("$module/$action");
        	elseif($module == '' && $action == '')
            	$url = '';
            
            if($url != '')
            	$url = "href=\"{$url}\"";
            
            // nome do menu
            $nome = $this->getArrayValue($menu, 'text');
            if(isset($menu['submenus']))
            	$retorno.= "<a data-toggle=\"dropdown\" class=\"dropdown-toggle\" {$url}>";
            else 
            	$retorno.= "<a {$url}>";
            $retorno.= $nome;
            $retorno.= (isset($menu['submenus'])) ? "<b class=\"caret\"></b>" : '';
            $retorno.= "</a>";

            if(isset($menu['submenus'])) 
            {
            	$retorno.= $this->carregarSubmenus($menu['submenus']);
            }
        }

        return $retorno;
    }
    
    private function carregarSubMenus($submenus)
    {
    	$retorno = "<ul class=\"dropdown-menu\">";
    	foreach($submenus as $submenu)
    	{	
    		// url
            $url = "";
            $module = $this->getArrayValue($submenu, 'module');
            $action = $this->getArrayValue($submenu, 'action');
        	if ($module != '' && $action == '')
                $url = url_for("$module");
            elseif($module != '' && $action != '')
            	$url = url_for("$module/$action");
        	elseif($module == '' && $action == '')
            	$url = '';
            if($url != '')
            	$url = "href=\"{$url}\"";
            
            	// nome do menu
            $nome = $this->getArrayValue($submenu, 'text');
    		$retorno.= "<li><a {$url}>{$nome}</a>";
    		if(isset($submenu['submenus']))
    			$retorno.= $this->carregarSubMenus($submenu['submenus']);
    		
    		$retorno.= "</li>";
    	}
    	$retorno.= "</ul>";
    	
    	return $retorno;
    }

    private function getArrayValue($array, $key, $addAspas = false)
    {
        $result = '';
        if (isset($array[$key])){
            $result = $array[$key];
        }

        return $addAspas ? $this->addAspas($result) : $result;
    }

    private function addAspas($string)
    {
        if ($string === ''){
            return "''";
        } else {
            return is_numeric($string) ? $string : "'$string'";
        }
    }
}